home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / scheme.el < prev    next >
Lisp/Scheme  |  1993-06-09  |  19KB  |  508 lines

  1. ;;; scheme.el --- Scheme mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Bill Rozas <jinz@prep.ai.mit.edu>
  6. ;; Keywords: languages, lisp
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; Adapted from Lisp mode by Bill Rozas, jinx@prep.
  27. ;; Initially a query replace of Lisp mode, except for the indentation 
  28. ;; of special forms.  Probably the code should be merged at some point 
  29. ;; so that there is sharing between both libraries.
  30.  
  31. ;;; Code:
  32.  
  33. (defvar scheme-mode-syntax-table nil "")
  34. (if (not scheme-mode-syntax-table)
  35.     (let ((i 0))
  36.       (setq scheme-mode-syntax-table (make-syntax-table))
  37.       (set-syntax-table scheme-mode-syntax-table)
  38.  
  39.       ;; Default is atom-constituent.
  40.       (while (< i 256)
  41.     (modify-syntax-entry i "_   ")
  42.     (setq i (1+ i)))
  43.  
  44.       ;; Word components.
  45.       (setq i ?0)
  46.       (while (<= i ?9)
  47.     (modify-syntax-entry i "w   ")
  48.     (setq i (1+ i)))
  49.       (setq i ?A)
  50.       (while (<= i ?Z)
  51.     (modify-syntax-entry i "w   ")
  52.     (setq i (1+ i)))
  53.       (setq i ?a)
  54.       (while (<= i ?z)
  55.     (modify-syntax-entry i "w   ")
  56.     (setq i (1+ i)))
  57.  
  58.       ;; Whitespace
  59.       (modify-syntax-entry ?\t "    ")
  60.       (modify-syntax-entry ?\n ">   ")
  61.       (modify-syntax-entry ?\f "    ")
  62.       (modify-syntax-entry ?\r "    ")
  63.       (modify-syntax-entry ?  "    ")
  64.  
  65.       ;; These characters are delimiters but otherwise undefined.
  66.       ;; Brackets and braces balance for editing convenience.
  67.       (modify-syntax-entry ?[ "(]  ")
  68.       (modify-syntax-entry ?] ")[  ")
  69.       (modify-syntax-entry ?{ "(}  ")
  70.       (modify-syntax-entry ?} "){  ")
  71.       (modify-syntax-entry ?\| "  23")
  72.  
  73.       ;; Other atom delimiters
  74.       (modify-syntax-entry ?\( "()  ")
  75.       (modify-syntax-entry ?\) ")(  ")
  76.       (modify-syntax-entry ?\; "<   ")
  77.       (modify-syntax-entry ?\" "\"    ")
  78.       (modify-syntax-entry ?' "  p")
  79.       (modify-syntax-entry ?` "  p")
  80.  
  81.       ;; Special characters
  82.       (modify-syntax-entry ?, "_ p")
  83.       (modify-syntax-entry ?@ "_ p")
  84.       (modify-syntax-entry ?# "_ p14")
  85.       (modify-syntax-entry ?\\ "\\   ")))
  86.  
  87. (defvar scheme-mode-abbrev-table nil "")
  88. (define-abbrev-table 'scheme-mode-abbrev-table ())
  89.  
  90. (defun scheme-mode-variables ()
  91.   (set-syntax-table scheme-mode-syntax-table)
  92.   (setq local-abbrev-table scheme-mode-abbrev-table)
  93.   (make-local-variable 'paragraph-start)
  94.   (setq paragraph-start (concat "^$\\|" page-delimiter))
  95.   (make-local-variable 'paragraph-separate)
  96.   (setq paragraph-separate paragraph-start)
  97.   (make-local-variable 'paragraph-ignore-fill-prefix)
  98.   (setq paragraph-ignore-fill-prefix t)
  99.   (make-local-variable 'indent-line-function)
  100.   (setq indent-line-function 'scheme-indent-line)
  101.   (make-local-variable 'comment-start)
  102.   (setq comment-start ";")
  103.   (make-local-variable 'comment-start-skip)
  104.   (setq comment-start-skip ";+[ \t]*")
  105.   (make-local-variable 'comment-column)
  106.   (setq comment-column 40)
  107.   (make-local-variable 'comment-indent-function)
  108.   (setq comment-indent-function 'scheme-comment-indent)
  109.   (setq mode-line-process '("" scheme-mode-line-process)))
  110.  
  111. (defvar scheme-mode-line-process "")
  112.  
  113. (defun scheme-mode-commands (map)
  114.   (define-key map "\t" 'scheme-indent-line)
  115.   (define-key map "\177" 'backward-delete-char-untabify)
  116.   (define-key map "\e\C-q" 'scheme-indent-sexp))
  117.  
  118. (defvar scheme-mode-map nil)
  119. (if (not scheme-mode-map)
  120.     (progn
  121.       (setq scheme-mode-map (make-sparse-keymap))
  122.       (scheme-mode-commands scheme-mode-map)))
  123.  
  124. ;;;###autoload
  125. (defun scheme-mode ()
  126.   "Major mode for editing Scheme code.
  127. Editing commands are similar to those of lisp-mode.
  128.  
  129. In addition, if an inferior Scheme process is running, some additional
  130. commands will be defined, for evaluating expressions and controlling
  131. the interpreter, and the state of the process will be displayed in the
  132. modeline of all Scheme buffers.  The names of commands that interact
  133. with the Scheme process start with \"xscheme-\".  For more information
  134. see the documentation for xscheme-interaction-mode.
  135.  
  136. Commands:
  137. Delete converts tabs to spaces as it moves back.
  138. Blank lines separate paragraphs.  Semicolons start comments.
  139. \\{scheme-mode-map}
  140. Entry to this mode calls the value of scheme-mode-hook
  141. if that value is non-nil."
  142.   (interactive)
  143.   (kill-all-local-variables)
  144.   (scheme-mode-initialize)
  145.   (scheme-mode-variables)
  146.   (run-hooks 'scheme-mode-hook))
  147.  
  148. (defun scheme-mode-initialize ()
  149.   (use-local-map scheme-mode-map)
  150.   (setq major-mode 'scheme-mode)
  151.   (setq mode-name "Scheme"))
  152.  
  153. (defvar scheme-mit-dialect t
  154.   "If non-nil, scheme mode is specialized for MIT Scheme.
  155. Set this to nil if you normally use another dialect.")
  156.  
  157. (defun scheme-comment-indent (&optional pos)
  158.   (save-excursion
  159.     (if pos (goto-char pos))
  160.     (cond ((looking-at ";;;") (current-column))
  161.       ((looking-at ";;")
  162.        (let ((tem (calculate-scheme-indent)))
  163.          (if (listp tem) (car tem) tem)))
  164.       (t
  165.        (skip-chars-backward " \t")
  166.        (max (if (bolp) 0 (1+ (current-column)))
  167.         comment-column)))))
  168.  
  169. (defvar scheme-indent-offset nil "")
  170. (defvar scheme-indent-function 'scheme-indent-function "")
  171.  
  172. (defun scheme-indent-line (&optional whole-exp)
  173.   "Indent current line as Scheme code.
  174. With argument, indent any additional lines of the same expression
  175. rigidly along with this one."
  176.   (interactive "P")
  177.   (let ((indent (calculate-scheme-indent)) shift-amt beg end
  178.     (pos (- (point-max) (point))))
  179.     (beginning-of-line)
  180.     (setq beg (point))
  181.     (skip-chars-forward " \t")
  182.     (if (looking-at "[ \t]*;;;")
  183.     ;; Don't alter indentation of a ;;; comment line.
  184.     nil
  185.       (if (listp indent) (setq indent (car indent)))
  186.       (setq shift-amt (- indent (current-column)))
  187.       (if (zerop shift-amt)
  188.       nil
  189.     (delete-region beg (point))
  190.     (indent-to indent))
  191.       ;; If initial point was within line's indentation,
  192.       ;; position after the indentation.  Else stay at same point in text.
  193.       (if (> (- (point-max) pos) (point))
  194.       (goto-char (- (point-max) pos)))
  195.       ;; If desired, shift remaining lines of expression the same amount.
  196.       (and whole-exp (not (zerop shift-amt))
  197.        (save-excursion
  198.          (goto-char beg)
  199.          (forward-sexp 1)
  200.          (setq end (point))
  201.          (goto-char beg)
  202.          (forward-line 1)
  203.          (setq beg (point))
  204.          (> end beg))
  205.        (indent-code-rigidly beg end shift-amt)))))
  206.  
  207. (defun calculate-scheme-indent (&optional parse-start)
  208.   "Return appropriate indentation for current line as scheme code.
  209. In usual case returns an integer: the column to indent to.
  210. Can instead return a list, whose car is the column to indent to.
  211. This means that following lines at the same level of indentation
  212. should not necessarily be indented the same way.
  213. The second element of the list is the buffer position
  214. of the start of the containing expression."
  215.   (save-excursion
  216.     (beginning-of-line)
  217.     (let ((indent-point (point)) state paren-depth desired-indent (retry t)
  218.       last-sexp containing-sexp first-sexp-list-p)
  219.       (if parse-start
  220.       (goto-char parse-start)
  221.     (beginning-of-defun))
  222.       ;; Find outermost containing sexp
  223.       (while (< (point) indent-point)
  224.     (setq state (parse-partial-sexp (point) indent-point 0)))
  225.       ;; Find innermost containing sexp
  226.       (while (and retry (setq paren-depth (car state)) (> paren-depth 0))
  227.     (setq retry nil)
  228.     (setq last-sexp (nth 2 state))
  229.     (setq containing-sexp (car (cdr state)))
  230.     ;; Position following last unclosed open.
  231.     (goto-char (1+ containing-sexp))
  232.     ;; Is there a complete sexp since then?
  233.     (if (and last-sexp (> last-sexp (point)))
  234.         ;; Yes, but is there a containing sexp after that?
  235.         (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
  236.           (if (setq retry (car (cdr peek))) (setq state peek))))
  237.     (if (not retry)
  238.         ;; Innermost containing sexp found
  239.         (progn
  240.           (goto-char (1+ containing-sexp))
  241.           (if (not last-sexp)
  242.           ;; indent-point immediately follows open paren.
  243.           ;; Don't call hook.
  244.           (setq desired-indent (current-column))
  245.         ;; Move to first sexp after containing open paren
  246.         (parse-partial-sexp (point) last-sexp 0 t)
  247.         (setq first-sexp-list-p (looking-at "\\s("))
  248.         (cond
  249.          ((> (save-excursion (forward-line 1) (point))
  250.              last-sexp)
  251.           ;; Last sexp is on same line as containing sexp.
  252.           ;; It's almost certainly a function call.
  253.           (parse-partial-sexp (point) last-sexp 0 t)
  254.           (if (/= (point) last-sexp)
  255.               ;; Indent beneath first argument or, if only one sexp
  256.               ;; on line, indent beneath that.
  257.               (progn (forward-sexp 1)
  258.                  (parse-partial-sexp (point) last-sexp 0 t)))
  259.           (backward-prefix-chars))
  260.          (t
  261.           ;; Indent beneath first sexp on same line as last-sexp.
  262.           ;; Again, it's almost certainly a function call.
  263.           (goto-char last-sexp)
  264.           (beginning-of-line)
  265.           (parse-partial-sexp (point) last-sexp 0 t)
  266.           (backward-prefix-chars)))))))
  267.       ;; If looking at a list, don't call hook.
  268.       (if first-sexp-list-p
  269.       (setq desired-indent (current-column)))
  270.       ;; Point is at the point to indent under unless we are inside a string.
  271.       ;; Call indentation hook except when overridden by scheme-indent-offset
  272.       ;; or if the desired indentation has already been computed.
  273.       (cond ((car (nthcdr 3 state))
  274.          ;; Inside a string, don't change indentation.
  275.          (goto-char indent-point)
  276.          (skip-chars-forward " \t")
  277.          (setq desired-indent (current-column)))
  278.         ((and (integerp scheme-indent-offset) containing-sexp)
  279.          ;; Indent by constant offset
  280.          (goto-char containing-sexp)
  281.          (setq desired-indent (+ scheme-indent-offset (current-column))))
  282.         ((not (or desired-indent
  283.               (and (boundp 'scheme-indent-function)
  284.                scheme-indent-function
  285.                (not retry)
  286.                (setq desired-indent
  287.                  (funcall scheme-indent-function
  288.                       indent-point state)))))
  289.          ;; Use default indentation if not computed yet
  290.          (setq desired-indent (current-column))))
  291.       desired-indent)))
  292.  
  293. (defun scheme-indent-function (indent-point state)
  294.   (let ((normal-indent (current-column)))
  295.     (save-excursion
  296.       (goto-char (1+ (car (cdr state))))
  297.       (re-search-forward "\\sw\\|\\s_")
  298.       (if (/= (point) (car (cdr state)))
  299.       (let ((function (buffer-substring (progn (forward-char -1) (point))
  300.                         (progn (forward-sexp 1) (point))))
  301.         method)
  302.         ;; Who cares about this, really?
  303.         ;(if (not (string-match "\\\\\\||" function)))
  304.         (setq function (downcase function))
  305.         (setq method (get (intern-soft function) 'scheme-indent-function))
  306.         (cond ((integerp method)
  307.            (scheme-indent-specform method state indent-point))
  308.           (method
  309.            (funcall method state indent-point))
  310.           ((and (> (length function) 3)
  311.             (string-equal (substring function 0 3) "def"))
  312.            (scheme-indent-defform state indent-point))))))))
  313.  
  314. (defvar scheme-body-indent 2 "")
  315.  
  316. (defun scheme-indent-specform (count state indent-point)
  317.   (let ((containing-form-start (car (cdr state))) (i count)
  318.     body-indent containing-form-column)
  319.     ;; Move to the start of containing form, calculate indentation
  320.     ;; to use for non-distinguished forms (> count), and move past the
  321.     ;; function symbol.  scheme-indent-function guarantees that there is at
  322.     ;; least one word or symbol character following open paren of containing
  323.     ;; form.
  324.     (goto-char containing-form-start)
  325.     (setq containing-form-column (current-column))
  326.     (setq body-indent (+ scheme-body-indent containing-form-column))
  327.     (forward-char 1)
  328.     (forward-sexp 1)
  329.     ;; Now find the start of the last form.
  330.     (parse-partial-sexp (point) indent-point 1 t)
  331.     (while (and (< (point) indent-point)
  332.         (condition-case nil
  333.             (progn
  334.               (setq count (1- count))
  335.               (forward-sexp 1)
  336.               (parse-partial-sexp (point) indent-point 1 t))
  337.           (error nil))))
  338.     ;; Point is sitting on first character of last (or count) sexp.
  339.     (cond ((> count 0)
  340.        ;; A distinguished form.  Use double scheme-body-indent.
  341.        (list (+ containing-form-column (* 2 scheme-body-indent))
  342.          containing-form-start))
  343.       ;; A non-distinguished form. Use body-indent if there are no
  344.       ;; distinguished forms and this is the first undistinguished
  345.       ;; form, or if this is the first undistinguished form and
  346.       ;; the preceding distinguished form has indentation at least
  347.       ;; as great as body-indent.
  348.       ((and (= count 0)
  349.         (or (= i 0)
  350.             (<= body-indent normal-indent)))
  351.        body-indent)
  352.       (t
  353.        normal-indent))))
  354.  
  355. (defun scheme-indent-defform (state indent-point)
  356.   (goto-char (car (cdr state)))
  357.   (forward-line 1)
  358.   (if (> (point) (car (cdr (cdr state))))
  359.       (progn
  360.     (goto-char (car (cdr state)))
  361.     (+ scheme-body-indent (current-column)))))
  362.  
  363. ;;; Let is different in Scheme
  364.  
  365. (defun would-be-symbol (string)
  366.   (not (string-equal (substring string 0 1) "(")))
  367.  
  368. (defun next-sexp-as-string ()
  369.   ;; Assumes that protected by a save-excursion
  370.   (forward-sexp 1)
  371.   (let ((the-end (point)))
  372.     (backward-sexp 1)
  373.     (buffer-substring (point) the-end)))
  374.  
  375. ;; This is correct but too slow.
  376. ;; The one below works almost always.
  377. ;;(defun scheme-let-indent (state indent-point)
  378. ;;  (if (would-be-symbol (next-sexp-as-string))
  379. ;;      (scheme-indent-specform 2 state indent-point)
  380. ;;      (scheme-indent-specform 1 state indent-point)))
  381.  
  382. (defun scheme-let-indent (state indent-point)
  383.   (skip-chars-forward " \t")
  384.   (if (looking-at "[a-zA-Z0-9+-*/?!@$%^&_:~]")
  385.       (scheme-indent-specform 2 state indent-point)
  386.       (scheme-indent-specform 1 state indent-point)))
  387.  
  388. ;; (put 'begin 'scheme-indent-function 0), say, causes begin to be indented
  389. ;; like defun if the first form is placed on the next line, otherwise
  390. ;; it is indented like any other form (i.e. forms line up under first).
  391.  
  392. (put 'begin 'scheme-indent-function 0)
  393. (put 'case 'scheme-indent-function 1)
  394. (put 'delay 'scheme-indent-function 0)
  395. (put 'do 'scheme-indent-function 2)
  396. (put 'lambda 'scheme-indent-function 1)
  397. (put 'let 'scheme-indent-function 'scheme-let-indent)
  398. (put 'let* 'scheme-indent-function 1)
  399. (put 'letrec 'scheme-indent-function 1)
  400. (put 'sequence 'scheme-indent-function 0)
  401.  
  402. (put 'call-with-input-file 'scheme-indent-function 1)
  403. (put 'with-input-from-file 'scheme-indent-function 1)
  404. (put 'with-input-from-port 'scheme-indent-function 1)
  405. (put 'call-with-output-file 'scheme-indent-function 1)
  406. (put 'with-output-to-file 'scheme-indent-function 1)
  407. (put 'with-output-to-port 'scheme-indent-function 1)
  408.  
  409. ;;;; MIT Scheme specific indentation.
  410.  
  411. (if scheme-mit-dialect
  412.     (progn
  413.       (put 'fluid-let 'scheme-indent-function 1)
  414.       (put 'in-package 'scheme-indent-function 1)
  415.       (put 'let-syntax 'scheme-indent-function 1)
  416.       (put 'local-declare 'scheme-indent-function 1)
  417.       (put 'macro 'scheme-indent-function 1)
  418.       (put 'make-environment 'scheme-indent-function 0)
  419.       (put 'named-lambda 'scheme-indent-function 1)
  420.       (put 'using-syntax 'scheme-indent-function 1)
  421.  
  422.       (put 'with-input-from-string 'scheme-indent-function 1)
  423.       (put 'with-output-to-string 'scheme-indent-function 0)
  424.       (put 'with-values 'scheme-indent-function 1)
  425.  
  426.       (put 'syntax-table-define 'scheme-indent-function 2)
  427.       (put 'list-transform-positive 'scheme-indent-function 1)
  428.       (put 'list-transform-negative 'scheme-indent-function 1)
  429.       (put 'list-search-positive 'scheme-indent-function 1)
  430.       (put 'list-search-negative 'scheme-indent-function 1)
  431.  
  432.       (put 'access-components 'scheme-indent-function 1)
  433.       (put 'assignment-components 'scheme-indent-function 1)
  434.       (put 'combination-components 'scheme-indent-function 1)
  435.       (put 'comment-components 'scheme-indent-function 1)
  436.       (put 'conditional-components 'scheme-indent-function 1)
  437.       (put 'disjunction-components 'scheme-indent-function 1)
  438.       (put 'declaration-components 'scheme-indent-function 1)
  439.       (put 'definition-components 'scheme-indent-function 1)
  440.       (put 'delay-components 'scheme-indent-function 1)
  441.       (put 'in-package-components 'scheme-indent-function 1)
  442.       (put 'lambda-components 'scheme-indent-function 1)
  443.       (put 'lambda-components* 'scheme-indent-function 1)
  444.       (put 'lambda-components** 'scheme-indent-function 1)
  445.       (put 'open-block-components 'scheme-indent-function 1)
  446.       (put 'pathname-components 'scheme-indent-function 1)
  447.       (put 'procedure-components 'scheme-indent-function 1)
  448.       (put 'sequence-components 'scheme-indent-function 1)
  449.       (put 'unassigned\?-components 'scheme-indent-function 1)
  450.       (put 'unbound\?-components 'scheme-indent-function 1)
  451.       (put 'variable-components 'scheme-indent-function 1)))
  452.  
  453. (defun scheme-indent-sexp ()
  454.   "Indent each line of the list starting just after point."
  455.   (interactive)
  456.   (let ((indent-stack (list nil)) (next-depth 0) bol
  457.     outer-loop-done inner-loop-done state this-indent)
  458.     (save-excursion (forward-sexp 1))
  459.     (save-excursion
  460.       (setq outer-loop-done nil)
  461.       (while (not outer-loop-done)
  462.     (setq last-depth next-depth
  463.           innerloop-done nil)
  464.     (while (and (not innerloop-done)
  465.             (not (setq outer-loop-done (eobp))))
  466.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  467.                       nil nil state))
  468.       (setq next-depth (car state))
  469.       (if (car (nthcdr 4 state))
  470.           (progn (indent-for-comment)
  471.              (end-of-line)
  472.              (setcar (nthcdr 4 state) nil)))
  473.       (if (car (nthcdr 3 state))
  474.           (progn
  475.         (forward-line 1)
  476.         (setcar (nthcdr 5 state) nil))
  477.         (setq innerloop-done t)))
  478.     (if (setq outer-loop-done (<= next-depth 0))
  479.         nil
  480.       (while (> last-depth next-depth)
  481.         (setq indent-stack (cdr indent-stack)
  482.           last-depth (1- last-depth)))
  483.       (while (< last-depth next-depth)
  484.         (setq indent-stack (cons nil indent-stack)
  485.           last-depth (1+ last-depth)))
  486.       (forward-line 1)
  487.       (setq bol (point))
  488.       (skip-chars-forward " \t")
  489.       (if (or (eobp) (looking-at "[;\n]"))
  490.           nil
  491.         (if (and (car indent-stack)
  492.              (>= (car indent-stack) 0))
  493.         (setq this-indent (car indent-stack))
  494.           (let ((val (calculate-scheme-indent
  495.               (if (car indent-stack) (- (car indent-stack))))))
  496.         (if (integerp val)
  497.             (setcar indent-stack
  498.                 (setq this-indent val))
  499.           (setcar indent-stack (- (car (cdr val))))
  500.           (setq this-indent (car val)))))
  501.         (if (/= (current-column) this-indent)
  502.         (progn (delete-region bol (point))
  503.                (indent-to this-indent)))))))))
  504.  
  505. (provide 'scheme)
  506.  
  507. ;;; scheme.el ends here
  508.